//
//  CollisionManager.m
//  Void Dead
//
//  Created by Sidney Just on 04.07.09.
//  Copyright 2009 by Sidney Just
//  This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; 
//  either version 3 of the License, or (at your option) any later version.
//  
//  This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty 
//  of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
//  

#import "CollisionManager.h"
#import "Engine.h"
#import "panel.h"
#import "TextureManager.h"
#import "VDead.h"


@implementation VD_CollisionManager

- (id)initWithEngine:(VD_Engine *)eng {
	if(self = [super init])
	{
		renderEngine = eng;
	}
	return self;
}

- (void)dealloc {
	[super dealloc];
}

- (BOOL)is2DCollisionBasedOnBoxBetween:(VD_Panel *)pan1 and:(VD_Panel *)pan2 {
	return ((pan1.pos_x < pan2.pos_x+pan2.size.x) && (pan1.pos_x+pan1.size.x > pan2.pos_x) &&
			(pan1.pos_y < pan2.pos_y+pan2.size.y) && (pan1.pos_y+pan1.size.y > pan2.pos_y));
}


- (BOOL)is2DCollisionBasedOnCircleBetween:(VD_Panel *)pan1 and:(VD_Panel *)pan2 {
	float rad1, rad2;
	VD_Vector *m1 = [[VD_Vector alloc] init];
	VD_Vector *m2 = [[VD_Vector alloc] init];
	
	if(pan1.size.x > pan1.size.y)
	{
		rad1 = pan1.size.x/2;
	} else {
		rad1 = pan1.size.y/2;
	}
	
	if(pan2.size.x > pan2.size.y)
	{
		rad2 = pan2.size.x/2;
	} else {
		rad2 = pan2.size.y/2;
	}
	
	m1.x = pan1.pos_x + (pan1.size.x / 2);
	m1.y = pan1.pos_y + (pan1.size.y / 2);
	
	m2.x = pan2.pos_x + (pan2.size.x / 2);
	m2.y = pan2.pos_y + (pan2.size.y / 2);
	
	float dist = vec_dist(m1, m2);
	
	[m1 release];
	[m2 release];
	
	if(dist > (rad1 + rad2))
	{
		return NO;
	} else { 
		return YES;
	}
}

@end
